//Demo
layui.use(['form', 'tree'], function() {
	var form = layui.form;
	var tree = layui.tree;
	var $ = layui.$;
	$("#treeHide").hide();
	//渲染
	//开启复选框
	var data1 = [{
		title: '江西',
		id: 1,
		children: [{
			title: '南昌',
			id: 1000,
			children: [{
				title: '青山湖区',
				id: 10001
			}, {
				title: '高新区',
				id: 10002
			}]
		}, {
			title: '九江',
			id: 1001
		}, {
			title: '赣州',
			id: 1002
		}]
	}, {
		title: '广西',
		id: 2,
		children: [{
			title: '南宁',
			id: 2000
		}, {
			title: '桂林',
			id: 2001
		}]
	}, {
		title: '陕西',
		id: 3,
		children: [{
			title: '西安',
			id: 3000
		}, {
			title: '延安',
			id: 3001
		}]
	}]

	//常规用法
	var parentMenu = {};
	tree.render({
		elem: '#test1',
		data: data1,
		showCheckbox: true,
		oncheck: function(obj) {
			if (obj.checked) {
				//复选框被选中传值
				parentMenu = obj.data;
			} else {
				parentMenu = {};
			}

			console.log(obj.data); //得到当前点击的节点数据
			console.log(obj.checked); //得到当前节点的展开状态：open、close、normal
			console.log(obj.elem); //得到当前节点元素

			console.log(obj.data.children); //当前节点下是否有子节点
		}
	});

	//监听提交
	form.on('submit(formDemo)', function(data) {
		return true;
	});

	var windowIndex = 0;
	$("input[name=parentMenu]").click(function() {
		var index = layer.open({
			type: 1,
			title: "菜单选择",
			area: ['400px', '550px'],
			content: $('#treeHide')
		})
		windowIndex = index;
	});

	$("#sure").click(function() {
		console.log(parentMenu);
		$("input[name=parentMenu]").val(parentMenu.title);
		$("input[name=parentMenu]").attr("parentId", parentMenu.id);
		layer.close(windowIndex);
	});

	$("#close").click(function() {
		layer.close(windowIndex);
	});





	form.on('radio(menuType)', function(data) {
		console.log(data.elem); //得到radio原始DOM对象
		console.log(data.value); //被点击的radio的value值
		if (data.value == 0) {
			$("#isHide_div").show();
			$("#requestUrl_div").hide();
			$("#roleKey_div").hide();
		}

		if (data.value == 1) {
			$("#requestUrl_div").show();
			$("#isHide_div").show();
			$("#roleKey_div").show();
		}

		if (data.value == 2) {
			$("#requestUrl_div").hide();
			$("#isHide_div").hide();
		}

	});
});
